<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

/**
 * Ajax action to save given block parameters into the given page
 */
class m_backoffice_actions_AjaxSaveBlockParamsAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$values = $this->getFormValues("form");
		
		$documentId = $values["pageId"];
		$documentLang = $values["pageLang"];
		$blockId = $values["blockId"];
		
		$tempDocService = m_backoffice_services_TempDocService::getInstance();
		
		if($tempDocService->hasTemporary(intval($documentId), $documentLang))
		{
			$page = $tempDocService->getTemporaryDocument($documentId, $documentLang);
		}
		else
		{
			$page = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Page', $documentId, $documentLang);
		}		
		
		$block = $page->getBlockById($blockId);
		
		foreach($values as $key => $value)
		{
			if($key != "pageId" && $key != "blockId")
			{
				if(is_array($value))
				{
					if(isset($value["vide"]) && !empty($value[0]))
					{
						unset($value["vide"]);
					}
					$block[$key] = join(',',$value);
				}
				else
				{
					$block[$key] = stripslashes($value);
				}
			}
		}

		$page->setBlockParamsById($blockId, $block);
		
		$tempDocService->saveTemporaryDocument($page);
		
		echo '<div id="formsuccess" style="display:none">blockEditRefresh</div>';
	}	
	
	public function errorHandler()
	{
		return '';
	}
}